package com.lwt.codetop.stack;
// 232. 用栈实现队列

import java.util.Stack;

/**
 * 思路：开两个栈（主栈和辅助栈）来模拟队列的先进先出特性
 *
 */
public class MyQueue {
    private Stack<Integer> stk;
    private Stack<Integer> w;
    public MyQueue(){
        stk = new Stack<>();
        w = new Stack<>();
    }

    public static void main(String[] args) {
        MyQueue queue = new MyQueue();

        // 测试用例
        System.out.println("队列是否为空: " + queue.empty()); // 应输出 true

        queue.push(1);
        queue.push(2);
        queue.push(3);

        System.out.println("队首元素: " + queue.peek()); // 应输出 1
        System.out.println("弹出元素: " + queue.pop());  // 应输出 1
        System.out.println("队首元素: " + queue.peek()); // 应输出 2

    }
    public void push(int x){
        stk.add(x);
    }

    /**
     * 用两个栈模拟实现队列的弹出操作
     * @return
     */
    public int pop(){
        while(stk.size() > 1){
            w.add(stk.pop());
        }
        int val = stk.pop();
        while (!w.isEmpty()){
            stk.add(w.pop());
        }
        return val;
    }

    /**
     * 用两个栈模拟获取队头元素
     * @return
     */
    public int peek() {
        while(stk.size() > 1){
            w.add(stk.pop());
        }
        int val = stk.peek();
        while (!w.isEmpty()){
            stk.add(w.pop());
        }
        return val;
    }
    public boolean empty(){
        return stk.isEmpty();
    }
}
